//
//  TelNoXmlParser.m
//  BosidengIphone
//
//  Created by  on 11-12-26.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "ItemsXmlParser.h"

@implementation ItemsXmlParser

-( id )init{
    
    if ( self =[ super init ]) {
        
        // 一个 groups 数组，代表了所有 List
        
        groups =[[ NSMutableArray alloc ] init ];
        
        [ _root setObject : groups forKey : @"items" ];
        
        _propertyNames = [[self propertyNames] retain];
        
    }
    
    return self ;
    
}

-( void )dealloc{
    
    [ groups release ], groups = nil ;
    [_currentCharacters release], _currentCharacters = nil;
    [_propertyNames release];
    [ super dealloc ];
    
}

//--------------------------------------------------------------//

#pragma mark -- libxml handler ，主要是 3 个回调方法 --

//--------------------------------------------------------------//

- (BOOL)isExpectName:(NSString *)name localName:(const xmlChar *)localname {
    NSString *key = [NSString stringWithCString:(char*)localname encoding:NSUTF8StringEncoding];
    return [name isEqualToString:key];
}

- (BOOL)isExpectNames:(NSArray *)names localName:(const xmlChar *)localname {
    NSString *key = [NSString stringWithCString:(char*)localname encoding:NSUTF8StringEncoding];
    for (NSString *name in names) {
        if ([name isEqualToString:key])
            return YES;
    }
    return NO;
}

- (NSString *)itemName {
    return @"item";
}

- (NSArray *)propertyNames {
    NSMutableArray *names = [NSMutableArray arrayWithCapacity:5];
    [names addObject:@"id"];
    return names;
}

// 解析元素开始标记时触发 , 在这里取元素的属性值以及设置标志变量

- ( void )startElementLocalName:( const xmlChar *)localname

                         prefix:( const xmlChar *)prefix

                            URI:( const xmlChar *)URI

                  nb_namespaces:( int )nb_namespaces

                     namespaces:( const xmlChar **)namespaces

                  nb_attributes:( int )nb_attributes

                   nb_defaulted:( int )nb_defaultedslo

                     attributes:( const xmlChar **)attributes

{ // 我们关心的元素标签
    
    //item
    //if (( strncmp ((char *)localname, "NewsList" , sizeof ( "NewsList" )) == 0 )) {
    if ([self isExpectName:[self itemName] localName:localname]) {
        _group =[[ NSMutableDictionary alloc ] init ];
        [ groups addObject : _group ]; // 把 group 加入数组
        //NSLog(@"%@ is begin founder!", [NSString stringWithCString:(char*)localname encoding:NSUTF8StringEncoding]);
    }
    
    //id
    if ([self isExpectNames:_propertyNames localName:localname]) {
        // 创建字符串
        [_currentCharacters release], _currentCharacters = nil;
        _currentCharacters = [[NSMutableString string] retain];
    }
}

// 解析元素结束标记时触发

- ( void )endElementLocalName:( const xmlChar *)localname

                       prefix:( const xmlChar *)prefix URI:( const xmlChar *)URI

{
    
    //id
    if ([self isExpectNames:_propertyNames localName:localname]) {
        NSString*   key;
        key = [NSString stringWithCString:(char*)localname encoding:NSUTF8StringEncoding];
        [_group setObject:_currentCharacters forKey:key];
        //NSLog(@"key=%@, _currentCharacters=%@", key, _currentCharacters);
        [_currentCharacters release], _currentCharacters = nil;
    }
    
    //item
    //if (( strncmp ((char *)localname, "NewsList" , sizeof ( "NewsList" )) == 0 )) {
    if ([self isExpectName:[self itemName] localName:localname]) {
        //NSLog(@"%@ is end founder!", [NSString stringWithCString:(char*)localname encoding:NSUTF8StringEncoding]);
        [ _group release ], _group = nil ; // 回收 _group 对象，以便重复利用
    }
}

// 解析元素体时触发

- ( void )charactersFound:( const xmlChar *)ch len:( int )len

{      
    // 添加解析到的字符串
    if (_currentCharacters) {
        NSString*   string;
        string = [[NSString alloc] initWithBytes:ch length:len encoding:NSUTF8StringEncoding];
        [_currentCharacters appendString:string];
        [string release];
    }
    
}

@end
